#include <stdio.h>
#include <stdlib.h>
#include "student.h"
#include <time.h>

void menu()
{
    printf("==========欢迎来到wgz的学生信息管理系统=================\n");
    printf("1: 添加学生\n");
    printf("2: 删除学生\n");
    printf("3: 修改学生\n");
    printf("4: 查询学生\n");
    printf("5: 列出所有学生\n");
    printf("6: 添加课程\n");
    printf("7: 删除课程\n");
    printf("8: 查询课程详细信息\n");
    printf("9: 修改课程信息\n");
    printf("10: 列出所有课程信息\n");
    printf("0: 退出\n"); // 退出并保存修改后的数据到student.txt和course.txt
    printf("请输入选项: ");
}

int main()
{
    Student *students = NULL;
    Course *courses = NULL;
    char filename_student[] = "../data/student.txt";
    char filename_course[] = "../data/course.txt";

    // 从文件中加载学生信息
    load_students_from_file(&students, filename_student, courses);
    load_courses_from_file(&courses, filename_course);

    while (1)
    {
        menu();
        int choice;
        scanf("%d", &choice);

        switch (choice)
        {
        case 1:
        {
            char id[10], name[50];
            printf("请输入学生的ID: \n");
            scanf("%s", id);
            if (contains_student(students, id))
            {
                printf("此ID已存在，请重新添加。\n");
                break;
            }
            printf("请输入学生的姓名: \n");
            scanf("%s", name);
            int count = 0;
            int flag = 0;
            char buffer[50];
            CourseIDArray course_ids;
            printf("请输入要添加的课程id：输入'end'来结束\n");
            while (1)
            {
                scanf("%s", buffer);
                if (strcmp(buffer, "end") == 0)
                {
                    break;
                }
                //为课程id分配内存
                course_ids[count] = (char *)malloc(strlen(buffer) + 1);
                if (course_ids[count] == NULL)
                {
                    perror("内存分配失败");
                    exit(EXIT_FAILURE);
                }

                strcpy(course_ids[count], buffer);
                memset(buffer, 0, sizeof(buffer)); // 清空缓冲区
                count++;
            }
            add_student(&students, id, name, courses, course_ids, count, flag);

            // 释放分配的内存
            for (int i = 0; i < count; ++i)
            {
                free(course_ids[i]);
            }

            break;
        }
        case 2:
        {
            char id[10];
            printf("请输入要删除的学生ID: ");
            scanf("%s", id);
            delete_student(&students, id);
            break;
        }
        case 3:
        {
            char id[10];
            printf("请输入要修改的学生ID: ");
            scanf("%s", id);
            update_student(&students, id);
            break;
        }
        case 4:
        {
            char id[10];
            printf("请输入要查询的学生ID: ");
            scanf("%s", id);
            find_student(students, id);
            break;
        }
        case 5:
        {
            // 列出所有学生
            list_students(students);
            break;
        }
        case 6:
        {
            // 添加课程信息
            char id[20], name[50], properties[50];
            int credits, selected_num, max_students;
            printf("请输入课程ID: ");
            scanf("%s", id);
            printf("请输入课程名称: ");
            scanf("%s", name);
            printf("请输入课程学分: ");
            scanf("%d", &credits);
            printf("请输入课程性质: ");
            scanf("%s", properties);
            printf("请输入已选人数: ");
            scanf("%d", &selected_num);
            printf("请输入人数上限: ");
            scanf("%d", &max_students);
            add_course(&courses, id, name, credits, properties, selected_num, max_students);
            break;
        }
        case 7:
        {
            // 删除课程
            char id[10];
            printf("请输入要删除的学生ID: ");
            scanf("%s", id);
            delete_course(&courses, id);
            break;
        }
        case 8:
        {
            // 查询课程详细信息
            char id[10];
            printf("请输入要查询的课程ID: ");
            scanf("%s", id);
            view_course(courses, id);
            break;
        }
        case 9:
        {
            // 修改课程信息
            char id[10];
            printf("请输入要修改的课程ID: ");
            scanf("%s", id);
            update_course(&courses, id);
            break;
        }
        case 10:
        {
            // 列出所有课程信息
            list_courses(courses);
            break;
        }
        case 0:
        {
            // 保存学生信息
            save_students_to_file(students, filename_student);
            save_courses_to_file(courses, filename_course);
            return 0;
        }
        default:
            printf("无效的选项，请重新选择。\n");
        }
    }

    // 释放所有分配的内存
    while (students != NULL)
    {
        Student *temp = students;
        students = students->next;

        // 释放课程链表
        while (temp->courses != NULL)
        {
            Course *course_temp = temp->courses;
            temp->courses = temp->courses->next;
            free(course_temp);
        }

        free(temp);
    }
    return 0;
}
